// 渠道配置模板接口
export interface ChannelConfigTemplate {
  channelCode: string
  baseConfigSchema: {
    [key: string]: {
      type: 'string' | 'number' | 'boolean' | 'object' | 'file'
      label: string
      required: boolean
      description?: string
      defaultValue?: any
      validation?: {
        pattern?: string
        min?: number
        max?: number
      }
    }
  }
  feeConfigSchema: {
    [key: string]: {
      type: 'string' | 'number' | 'boolean' | 'object' | 'file'
      label: string
      required: boolean
      description?: string
      defaultValue?: any
      validation?: {
        pattern?: string
        min?: number
        max?: number
      }
    }
  }
}

// 微信支付配置模板
const wechatPayTemplate: ChannelConfigTemplate = {
  channelCode: 'WECHAT',
  baseConfigSchema: {
    mchId: {
      type: 'string',
      label: '商户号',
      required: false,
      description: '微信支付商户号'
    },
    mchApiKeyV2: {
      type: 'string',
      label: '商户API密钥V2',
      required: false,
      description: '微信支付商户API密钥V2'
    },
    mchApiKeyV3: {
      type: 'string',
      label: '商户API密钥V3',
      required: false,
      description: '微信支付商户API密钥V3'
    },
    mchSerialNo: {
      type: 'string',
      label: '商户证书序列号',
      required: false,
      description: '微信支付商户证书序列号'
    },
    certPath: {
      type: 'string',
      label: '商户API证书路径',
      required: false,
      description: '微信支付商户API证书路径'
    },
    certPassword: {
      type: 'string',
      label: '商户API证书密码',
      required: false,
      description: '微信支付商户API证书密码'
    },
    privateKeyPath: {
      type: 'string',
      label: '商户私钥路径',
      required: false,
      description: '微信支付商户私钥路径'
    },
    publicKeyPath: {
      type: 'string',
      label: '商户公钥路径',
      required: false,
      description: '微信支付商户公钥路径'
    },
    platformCertPath: {
      type: 'string',
      label: '平台证书路径',
      required: false,
      description: '微信支付平台证书路径'
    },
    notifyUrl: {
      type: 'string',
      label: '回调通知地址',
      required: false,
      description: '支付结果通知地址'
    },
    apiVersion: {
      type: 'string',
      label: 'API版本',
      required: false,
      description: '微信支付API版本',
      defaultValue: 'v3'
    }
  },
  feeConfigSchema: {
    rate: {
      type: 'number',
      label: '费率',
      required: false,
      description: '交易费率',
      defaultValue: 0.006,
      validation: {
        min: 0,
        max: 1
      }
    },
    minFee: {
      type: 'number',
      label: '最低手续费',
      required: false,
      description: '最低手续费金额',
      defaultValue: 0.01,
      validation: {
        min: 0
      }
    },
    maxFee: {
      type: 'number',
      label: '最高手续费',
      required: false,
      description: '最高手续费金额',
      defaultValue: 1000,
      validation: {
        min: 0
      }
    }
  }
}

// 支付宝配置模板
const alipayTemplate: ChannelConfigTemplate = {
  channelCode: 'ALIPAY',
  baseConfigSchema: {
    appId: {
      type: 'string',
      label: '应用ID',
      required: false,
      description: '支付宝应用ID'
    },
    privateKey: {
      type: 'string',
      label: '应用私钥',
      required: false,
      description: '支付宝应用私钥'
    },
    publicKey: {
      type: 'string',
      label: '支付宝公钥',
      required: false,
      description: '支付宝公钥'
    },
    notifyUrl: {
      type: 'string',
      label: '回调通知地址',
      required: false,
      description: '支付结果通知地址'
    }
  },
  feeConfigSchema: {
    rate: {
      type: 'number',
      label: '费率',
      required: false,
      description: '交易费率',
      defaultValue: 0.006,
      validation: {
        min: 0,
        max: 1
      }
    },
    minFee: {
      type: 'number',
      label: '最低手续费',
      required: false,
      description: '最低手续费金额',
      defaultValue: 0.01,
      validation: {
        min: 0
      }
    },
    maxFee: {
      type: 'number',
      label: '最高手续费',
      required: false,
      description: '最高手续费金额',
      defaultValue: 1000,
      validation: {
        min: 0
      }
    }
  }
}

// 乐刷支付配置模板
const leshuaTemplate: ChannelConfigTemplate = {
  channelCode: 'LESHUA',
  baseConfigSchema: {
    appId: {
      type: 'string',
      label: '应用ID',
      required: false,
      description: '乐刷支付应用ID'
    },
    mchId: {
      type: 'string',
      label: '商户号',
      required: false,
      description: '乐刷支付商户号'
    },
    apiKey: {
      type: 'string',
      label: 'API密钥',
      required: false,
      description: '乐刷支付API密钥'
    },
    notifyUrl: {
      type: 'string',
      label: '回调通知地址',
      required: false,
      description: '支付结果通知地址'
    }
  },
  feeConfigSchema: {
    rate: {
      type: 'number',
      label: '费率',
      required: false,
      description: '交易费率',
      defaultValue: 0.006,
      validation: {
        min: 0,
        max: 1
      }
    },
    minFee: {
      type: 'number',
      label: '最低手续费',
      required: false,
      description: '最低手续费金额',
      defaultValue: 0.01,
      validation: {
        min: 0
      }
    },
    maxFee: {
      type: 'number',
      label: '最高手续费',
      required: false,
      description: '最高手续费金额',
      defaultValue: 1000,
      validation: {
        min: 0
      }
    }
  }
}

// 斗拱支付配置模板
const douGongTemplate: ChannelConfigTemplate = {
  channelCode: 'DOUGONG',
  baseConfigSchema: {
    appId: {
      type: 'string',
      label: '应用ID',
      required: false,
      description: '斗拱支付应用ID'
    },
    mchId: {
      type: 'string',
      label: '商户号',
      required: false,
      description: '斗拱支付商户号'
    },
    apiKey: {
      type: 'string',
      label: 'API密钥',
      required: false,
      description: '斗拱支付API密钥'
    },
    secretKey: {
      type: 'string',
      label: '签名密钥',
      required: false,
      description: '斗拱支付签名密钥'
    },
    notifyUrl: {
      type: 'string',
      label: '回调通知地址',
      required: false,
      description: '支付结果通知地址'
    },
    apiUrl: {
      type: 'string',
      label: 'API接口地址',
      required: false,
      description: '斗拱支付API接口地址',
      defaultValue: 'https://api.dougong.com'
    },
    certFile: {
      type: 'file',
      label: '证书文件',
      required: false,
      description: '上传斗拱支付证书文件'
    }
  },
  feeConfigSchema: {
    rate: {
      type: 'number',
      label: '费率',
      required: false,
      description: '交易费率',
      defaultValue: 0.006,
      validation: {
        min: 0,
        max: 1
      }
    },
    minFee: {
      type: 'number',
      label: '最低手续费',
      required: false,
      description: '最低手续费金额',
      defaultValue: 0.01,
      validation: {
        min: 0
      }
    },
    maxFee: {
      type: 'number',
      label: '最高手续费',
      required: false,
      description: '最高手续费金额',
      defaultValue: 1000,
      validation: {
        min: 0
      }
    }
  }
}

// 渠道配置模板映射
export const channelTemplates: { [key: string]: ChannelConfigTemplate } = {
  WECHAT: wechatPayTemplate,
  ALIPAY: alipayTemplate,
  LESHUA: leshuaTemplate,
  DOUGONG: douGongTemplate
}

// 获取渠道配置模板
export function getChannelTemplate(channelCode: string): ChannelConfigTemplate | undefined {
  return channelTemplates[channelCode]
}

// 验证配置是否符合模板要求
export function validateConfig(config: any, template: ChannelConfigTemplate): { valid: boolean; errors: string[] } {
  const errors: string[] = []

  // 验证基础配置
  for (const [key, schema] of Object.entries(template.baseConfigSchema)) {
    if (schema.required && !config[key]) {
      errors.push(`${schema.label}不能为空`)
    }

    if (config[key] && schema.validation) {
      if (schema.type === 'number') {
        const value = Number(config[key])
        if (schema.validation.min !== undefined && value < schema.validation.min) {
          errors.push(`${schema.label}不能小于${schema.validation.min}`)
        }
        if (schema.validation.max !== undefined && value > schema.validation.max) {
          errors.push(`${schema.label}不能大于${schema.validation.max}`)
        }
      }

      if (schema.validation.pattern && !new RegExp(schema.validation.pattern).test(config[key])) {
        errors.push(`${schema.label}格式不正确`)
      }
    }
  }

  return {
    valid: errors.length === 0,
    errors
  }
}